package z_mxsTest.testServiceConfigMergeCase;

//import ma.xin.shi.configurations.*;
import ma.xin.shi.configurations.serviceMerge.ServiceConfigMergeCase;
import ma.xin.shi.configurations.serviceMerge.ServiceEntryMergeCase;

import java.util.List;        // 添加这行
//import java.util.ArrayList;   // 如果你也用了 ArrayList，也加上

public class ServiceConfigMergeCaseTest {
    public static void main(String[] args) {
        ServiceConfigMergeCase serviceConfig = new ServiceConfigMergeCase();

        // Step 1: 添加三个不同类型的服务
        System.out.println("=== Step 1: Adding services ===");
        addMethodService(serviceConfig);
        addEventService(serviceConfig);
        addFieldService(serviceConfig);

        printAllServices(serviceConfig);

        // Step 2: 修改 Method 类型服务的参数
        System.out.println("\n=== Step 2: Updating Method Service ===");
        updateMethodService(serviceConfig, "LoginService", "authenticate", 0x10, false);
        printAllServices(serviceConfig);

        // Step 3: 修改 Event 类型服务的参数
        System.out.println("\n=== Step 3: Updating Event Service ===");
        updateEventService(serviceConfig, "TemperatureEvent", "onHighTemp", 0x20, "float temperature");
        printAllServices(serviceConfig);

        // Step 4: 修改 Field 类型服务的参数
        System.out.println("\n=== Step 4: Updating Field Service ===");
        updateFieldService(serviceConfig, "UserInfoField", "email", "string");
        printAllServices(serviceConfig);

        // Step 5: 删除 Event 类型服务
        System.out.println("\n=== Step 5: Deleting Event Service ===");
        deleteService(serviceConfig, "TemperatureEvent");
        printAllServices(serviceConfig);
    }

    private static void addMethodService(ServiceConfigMergeCase config) {
        ServiceEntryMergeCase entry = new ServiceEntryMergeCase("LoginService", 0x0100, ServiceEntryMergeCase.RpcType.METHOD);
        entry.setMethodName("login");
        entry.setMethodId(0x01);
        entry.setRrFlag(true);
        config.addService(entry);
        System.out.println("Added Method Service: " + entry.getServiceName());
    }

    private static void addEventService(ServiceConfigMergeCase config) {
        ServiceEntryMergeCase entry = new ServiceEntryMergeCase("TemperatureEvent", 0x0200, ServiceEntryMergeCase.RpcType.EVENT);
        entry.setEventName("onTempChange");
        entry.setEventId(0x02);
        entry.setEventParameter("float temp");
        config.addService(entry);
        System.out.println("Added Event Service: " + entry.getServiceName());
    }

    private static void addFieldService(ServiceConfigMergeCase config) {
        ServiceEntryMergeCase entry = new ServiceEntryMergeCase("UserInfoField", 0x0300, ServiceEntryMergeCase.RpcType.FIELD);
        entry.setFieldName("userName");
        entry.setFieldDataType("string");
        config.addService(entry);
        System.out.println("Added Field Service: " + entry.getServiceName());
    }

    private static void updateMethodService(ServiceConfigMergeCase config, String name, String newMethodName,
                                            int newMethodId, boolean newRRFlag) {
        ServiceEntryMergeCase entry = config.getServiceByName(name);
        if (entry != null && entry.getRpcType() == ServiceEntryMergeCase.RpcType.METHOD) {
            entry.setMethodName(newMethodName);
            entry.setMethodId(newMethodId);
            entry.setRrFlag(newRRFlag);
            System.out.println("Updated Method Service: " + name);
        } else {
            System.out.println("Failed to find or invalid type for Method Service: " + name);
        }
    }

    private static void updateEventService(ServiceConfigMergeCase config, String name, String newEventName,
                                           int newEventId, String newEventParam) {
        ServiceEntryMergeCase entry = config.getServiceByName(name);
        if (entry != null && entry.getRpcType() == ServiceEntryMergeCase.RpcType.EVENT) {
            entry.setEventName(newEventName);
            entry.setEventId(newEventId);
            entry.setEventParameter(newEventParam);
            System.out.println("Updated Event Service: " + name);
        } else {
            System.out.println("Failed to find or invalid type for Event Service: " + name);
        }
    }

    private static void updateFieldService(ServiceConfigMergeCase config, String name, String newFieldName,
                                           String newFieldType) {
        ServiceEntryMergeCase entry = config.getServiceByName(name);
        if (entry != null && entry.getRpcType() == ServiceEntryMergeCase.RpcType.FIELD) {
            entry.setFieldName(newFieldName);
            entry.setFieldDataType(newFieldType);
            System.out.println("Updated Field Service: " + name);
        } else {
            System.out.println("Failed to find or invalid type for Field Service: " + name);
        }
    }

    private static void deleteService(ServiceConfigMergeCase config, String name) {
        boolean removed = config.removeService(name);
        if (removed) {
            System.out.println("Successfully deleted Service: " + name);
        } else {
            System.out.println("Failed to delete Service: " + name);
        }
    }

    private static void printAllServices(ServiceConfigMergeCase config) {
        System.out.println("\n--- Current Services ---");
        List<ServiceEntryMergeCase> entries = config.getAllServices();
        if (entries.isEmpty()) {
            System.out.println("No services configured.");
        } else {
            for (ServiceEntryMergeCase entry : entries) {
                System.out.println(entry.toString());
            }
        }
    }
}
